package com.java.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @ClassNameEs_Client
 * @Description
 * @Author liufei
 * @Date2021/12/8 10:32
 * @Version V1.0
 **/
public class Es_Client_Doc_Query {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );
        //查询数据
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //条件查询 termQuery
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //分页查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //(当前页码-1)*每页显示条数
        builder.from(2);
        builder.size(2);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //查询排序
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //(当前页码-1)*每页显示条数
        builder.sort("age", SortOrder.DESC);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //过滤字段
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        String[]excludes={};
        String[]includes={"name","age"};
        builder.fetchSource(includes,excludes);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //组合查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
        //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","男"));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age",30));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age",40));

        builder.query(boolQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //范围查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        //大于等于30
        rangeQueryBuilder.gte(30);
        //小于等于40
        rangeQueryBuilder.lte(40);
        builder.query(rangeQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //模糊查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "lisi").fuzziness(Fuzziness.TWO);

        builder.query(fuzzyQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //高亮查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "lisi");
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        builder.highlighter(highlightBuilder);
        builder.query(termQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //聚合查询
        /*SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        builder.aggregation(aggregationBuilder);
        //builder.query(termQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println(search);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }*/
        //分组查询
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(aggregationBuilder);
        //builder.query(termQueryBuilder);
        request.source(builder);
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println(search);
        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(search.getTook());
        for (SearchHit hit : hits
        ) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭es客户端
        esClient.close();
    }
}
